<?php
    session_start();
	require('../basicInfo/user.php');

    //set the current page in the seesion
    $UserNameIsEmpty = false;
    $passwordIsEmpty = false;

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if ($_POST["uname"]=="") {
        $UserNameIsEmpty = true;
    }
    if ($_POST["password"]=="")
        $passwordIsEmpty = true;


if(!$UserNameIsEmpty && !$passwordIsEmpty)
$userId = UserController::get_user_id_by_username_password($_POST["uname"],$_POST["password"]);

if ($userId) {
    $userinfo=UserController::obtain_user($_POST["uname"]);
      $serialzedUser = serialize($userinfo);
        $_SESSION['userinfo']=$serialzedUser;
        $_SESSION['logged_in']="true";
        echo '<script language="javascript">location.replace("profile.php");</script>';
        //header('Location: profile.php' );
        exit;
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
    $fullr = array("User_Name" => $_POST["uname"],
        "Password" => $_POST["password"]);
else
    $fullr = array("User_Name"=>"",
        "Password" => "");

if(isset($_SESSION['userinfo'])){
    //declaration of variables
    $serializedUser = $_SESSION['userinfo'];
    $unserializedUser = unserialize($serializedUser);
    $userName = NULL;
    $firstName = NULL;
    $lastName = NULL;
    $emailAddress = NULL;
    $experience = NULL;
    $gold = NULL;
    $qCreated = NULL;
    $bday = NULL;

    if(!empty ($_SERVER['QUERY_STRING']))
    {
        $_SESSION['currentPage'] = 'friends';
        $qStrArr = explode("&" ,$_SERVER['QUERY_STRING'] );
        $userArr = explode("=" ,$qStrArr[0] );
        $userName = $userArr[1];

        //retrieve the other details from the db on the basis of user


    }
    else
    {
         $_SESSION['currentPage'] = 'profile';

       //uncomment the below code to retrieve  the values from session object
       $userName = $unserializedUser->getUserName();
       $firstName = $unserializedUser->getFirstName();
       $lastName = $unserializedUser->getLastName();
       $emailAddress = $unserializedUser->getEmail();
       $bday = $unserializedUser->getBday();
       $experience = $unserializedUser->getExp();
       $gold = $unserializedUser->getGold();
       $qCreated = $unserializedUser->getQCreated();

     }

}
 ?>

<?php include "../../layout/header.php"; ?>
<?php include "../../layout/navleft.php"; ?>
<div id="content_small">
	<div class="container_brown">
		<div class="container_brown_title">Profile</div>
		<div class="profile_div">
		<?php if(isset($_SESSION['userinfo'])){?>
		    <div id="profile_heading">WelCome <?php print $userName;  ?> </div>
		
		    <div class="profile_text">First Name : <?php print  $firstName; ?></div>
		    <div class="profile_text">Last Name : <?php print  $lastName ?></div>
		    <div class="profile_text">Email Address : <?php print  $emailAddress; ?></div>
		    <div class="profile_text">Experience : <?php print  $experience; ?></div>
		    <br/>
		    <div class="profile_text">Gold : <?php print  $gold; ?></div>
		    <br/>
		    <div class="profile_text">Questions Created : <?php print  $qCreated; ?></div>
		
		<?php }else{?>
			<div class="error_text">User Name or Password is not correct!</div>
		<?php }?>
		</div>
	</div>
</div>
<?php include "../../layout/navright.php"; ?>
<?php include "../../layout/footer.php"; ?>
